#include "trackballcamera.h"
#include "graphisme.h"

#include <vector>

using namespace std;

GLfloat redMaterial[] = {1.0, 0.0, 0.0,1};
GLfloat blackMaterial[] = {0.0, 0.0, 0.0,1};
GLfloat blueMaterial[] = {0.0, 0.0, 0.2,1};
GLfloat purpleMaterial[] = {0.47, 0.047, 0.486,1};
GLfloat yellowMaterial[] = {1.0, 1.0, 0.0, 1};
GLfloat greenMaterial[] = {0.36, 0.875, 0.06, 0.4};

void stop(TrackBallCamera *camera )
{
    delete camera;
    SDL_Quit();
}

void init_dessin(TrackBallCamera *camera )
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity( );
	
	camera->look();	
}

void fin_dessin()
{

	glFlush();
    SDL_GL_SwapBuffers();
	
}

void dessin_cube_transparent(int x, int y, int z)
{
	
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, greenMaterial);
	glTranslated(2*x-5,2*y-7,2*z-5);
	
	glBegin(GL_QUADS);
    glVertex3d(1,1,1);
    glVertex3d(1,1,-1);
    glVertex3d(-1,1,-1);
    glVertex3d(-1,1,1);

    glVertex3d(1,-1,1);
    glVertex3d(1,-1,-1);
    glVertex3d(1,1,-1);
    glVertex3d(1,1,1);

    glVertex3d(-1,-1,1);
    glVertex3d(-1,-1,-1);
    glVertex3d(1,-1,-1);
    glVertex3d(1,-1,1);

    glVertex3d(-1,1,1);
    glVertex3d(-1,1,-1);
    glVertex3d(-1,-1,-1);
    glVertex3d(-1,-1,1);

    glVertex3d(1,1,-1);
    glVertex3d(1,-1,-1);
    glVertex3d(-1,-1,-1);
    glVertex3d(-1,1,-1);

    glVertex3d(1,-1,1);
    glVertex3d(1,1,1);
    glVertex3d(-1,1,1);
    glVertex3d(-1,-1,1);

    glEnd();

    glTranslated(-2*x+5,-2*y+7,-2*z+5);

}

void dessin_zone(vector<int> *zone)
{
	//~ glBlendFunc(GL_ONE, GL_ONE);
	int i;
	for(i=0; i<zone->size() ; i+=3)
	{
		dessin_cube_transparent((*zone)[i], (*zone)[i+1], (*zone)[i+2]);
	}
	//~ glFlush();
	//~ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void dessin_pion(int x, int y, int z, int couleur, GLUquadric* params)
{
if (couleur == 1)
{
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, redMaterial);
}
else
{
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blueMaterial);
}

glTranslated(2*x-5,2*y-7,2*z-6);
gluCylinder(params,0.5,0.5,0.8,20,1);
gluDisk(params, 0, 0.5,20,1);

glTranslated(0,0,0.8);
gluDisk(params, 0, 0.5,20,1);

glTranslated(0,0,0.4);
gluSphere(params,0.4,20,20);

glTranslated(-2*x+5, -2*y +7, -2*z+6 -0.8 -0.4);

}

void dessin_tour(int x, int y, int z, int couleur, GLUquadric* params)
{
	if (couleur == 1)
	{
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, redMaterial);

	}
	else
	{
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blueMaterial);
	}
	
glTranslated(2*x-5,2*y-7,2*z-6);

gluDisk(params, 0, 0.7,20,1);
gluCylinder(params,0.7,0,1,20,1);
gluCylinder(params,0.5,0.5,1.2,20,1);

glTranslated(0,0,0.5);
gluCylinder(params,0,0.7,1,20,1);
glTranslated(0,0,1);
gluDisk(params, 0, 0.7,20,1);

glTranslated(-2*x+5, -2*y +7, -2*z+6 -1.5 );

}

void dessin_fou(int x, int y, int z, int couleur, GLUquadric* params)
{
	if (couleur == 1)
{
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, redMaterial);
}
else
{
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blueMaterial);
}
glTranslated(2*x-5,2*y-7,2*z-6);
gluCylinder(params,0.7,0,1,20,1);
gluDisk(params, 0, 0.7,20,1);

glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, purpleMaterial);
glTranslated(0,0,1);
gluCylinder(params,0.4,0,0.8,20,1);
gluDisk(params, 0, 0.4,20,1);

glTranslated(-2*x+5, -2*y +7, -2*z+6 -1);
}

void dessin_cavalier(int x, int y, int z, int couleur, GLUquadric* params)
{
	
glTranslated(2*x-5,2*y-7,2*z-6);

		if (couleur == 1)
	{
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, purpleMaterial);
		gluDisk(params, 0, 0.5,20,1);
		gluCylinder(params,0.5,0,1.4,20,1);
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, redMaterial);
		glTranslated(0,-0.4,1.4);
		glRotated(-110,1,0,0);
		gluCylinder(params,0.4,0,1.2,20,1);
		gluDisk(params, 0, 0.4,20,1);
		glRotated(110,1,0,0);
		glTranslated(-2*x+5, -2*y +7 + 0.4, -2*z+6 -1.4);
	}
	else
	{
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, purpleMaterial);
		gluDisk(params, 0, 0.5,20,1);
		gluCylinder(params,0.5,0,1.4,20,1);
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blueMaterial);
		glTranslated(0,0.4,1.4);
		glRotated(110,1,0,0);
		gluCylinder(params,0.4,0,1.2,20,1);
		gluDisk(params, 0, 0.4,20,1);
		glRotated(-110,1,0,0);
		glTranslated(-2*x+5, -2*y +7 -0.4, -2*z+6 -1.4);
	}
}

void dessin_roi(int x, int y, int z, int couleur, GLUquadric* params)
{
		if (couleur == 1)
	{
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, redMaterial);
	}
	else
	{
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blueMaterial);
	}
glTranslated(2*x-5,2*y-7,2*z-6);
gluCylinder(params,0,0.5,1,20,1);
glTranslated(0,0,1);
gluDisk(params, 0, 0.5,20,1);
glTranslated(0,0,0.4);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, purpleMaterial);
gluSphere(params,0.4,20,20);
glTranslated(0,0,0.4);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, yellowMaterial);
gluCylinder(params,0.4,0.4,0.1,20,1);
glTranslated(-2*x+5, -2*y +7, -2*z+6 -1.8);

}


void dessin_dame(int x, int y, int z, int couleur, GLUquadric* params)
{
	if (couleur == 1)
{
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, redMaterial);
}
else
{
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blueMaterial);
}
glTranslated(2*x-5,2*y-7,2*z-6);
gluDisk(params, 0, 0.5,20,1);
gluCylinder(params,0,0.5,1,20,1);
gluCylinder(params,0.5,0,1,20,1);
glTranslated(0,0,1);
gluDisk(params, 0, 0.5,20,1);
glTranslated(0,0,0.4);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, purpleMaterial);
gluSphere(params,0.4,20,20);
glTranslated(-2*x+5, -2*y +7, -2*z+6 -1.4);

}

void dessine_echiquier()
{
	for (int i = 0; i<6;i++)
	{
		for(int j = 0; j<8;j++)
		{
			for(int k = 0; k<6;k++)
			{
				dessine_cube(2*i-5,2*j-7,2*k-5);
			}
		}
	}
}

void init_echiquier(GLUquadric* params)
{
	for (int i = 0; i<6;i++)
	{
		for (int j = 0; j<6; j++)
		{
			dessin_pion(i,1,j, 0, params);
			dessin_pion(i,6,j, 1, params);
		}
	}
	
	dessin_tour(0,0,0,0, params);
	dessin_tour(0,0,5,0, params);
	dessin_tour(5,0,5,0, params);
	dessin_tour(5,0,0,0, params);
	
	dessin_tour(0,7,0,1, params);
	dessin_tour(0,7,5,1, params);
	dessin_tour(5,7,5,1, params);
	dessin_tour(5,7,0,1, params);
	
	dessin_cavalier(1,0,1,0,params);
	dessin_cavalier(1,0,4,0,params);
	dessin_cavalier(4,0,4,0,params);
	dessin_cavalier(4,0,1,0,params);
	
	dessin_cavalier(1,7,1,1,params);
	dessin_cavalier(1,7,4,1,params);
	dessin_cavalier(4,7,4,1,params);
	dessin_cavalier(4,7,1,1,params);
	
	dessin_fou(2,0,2,0,params);
	dessin_fou(3,0,2,0,params);
	
	dessin_fou(2,7,2,1,params);
	dessin_fou(3,7,2,1,params);
	
	dessin_dame(2,0,3,0,params);
	dessin_roi(3,0,3,0,params);
	
	dessin_dame(3,7,3,1,params);
	dessin_roi(2,7,3,1,params);

}
				
void dessine_cube (int x, int y, int z)
{
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blackMaterial);

	glBegin(GL_LINE_STRIP);
    
    glVertex3d(1+x,1+y,1+z);
    glVertex3d(1+x,1+y,-1+z);
    glVertex3d(-1+x,1+y,-1+z);
    glVertex3d(-1+x,1+y,1+z);
    glVertex3d(1+x,1+y,1+z);
    
    glVertex3d(1+x,-1+y,1+z);
    glVertex3d(1+x,-1+y,-1+z);
    glVertex3d(1+x,1+y,-1+z);
    glVertex3d(1+x,1+y,1+z);
    
    glEnd();
    
    glBegin(GL_LINE_STRIP);

    glVertex3d(-1+x,-1+y,1+z);
    glVertex3d(-1+x,-1+y,-1+z);
    glVertex3d(1+x,-1+y,-1+z);
    glVertex3d(1+x,-1+y,1+z);
    glVertex3d(-1+x,-1+y,1+z);

    glVertex3d(-1+x,1+y,1+z);
    glVertex3d(-1+x,1+y,-1+z);
    glVertex3d(-1+x,-1+y,-1+z);
    glVertex3d(-1+x,-1+y,1+z);
    
    glEnd();
}

